/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2012-10-24         luwei
 */

package com.tianci.appstore.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;

import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyCategoryData;
import com.tianci.appstore.utils.SkyConstants;

import java.util.ArrayList;
import java.util.List;

public class SkyCategoryName extends View {
    private Paint mPaint = new Paint();
    private int mCurrentPos = 0;
    private int mHSpace = 45;
    private int mOffsetX = 60;
    private int mOffsetY = 67;
    private int mDivideOffsetY = 10;
    private int mBeginX = -50;
    private int mRedTextSize = 45;
    private int mCommonTextSize = 60;
    private List<Category> mCategories = null;
    private Context mContext = null;
    private Bitmap mRedBitmap = null;
    private SparseArray<Integer> mRedPoint = new SparseArray<Integer>();

    public SkyCategoryName(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        mPaint.setAntiAlias(true);
        init();
    }

    public SkyCategoryName(Context context) {
        super(context);
        this.mContext = context;
        mPaint.setAntiAlias(true);
        init();
    }

    private void init() {
        mHSpace = SkyConstants.Div(mHSpace);
        mOffsetX = SkyConstants.Div(mOffsetX);
        mOffsetY = SkyConstants.Div(mOffsetY);
        mDivideOffsetY = SkyConstants.Div(mDivideOffsetY);
        mBeginX = SkyConstants.Div(mBeginX);
        mRedTextSize = SkyConstants.Dpi(mRedTextSize);
        mCommonTextSize = SkyConstants.Div(38);
        mPaint.setColor(Color.WHITE);
        LogUtils.d("the common textSize is " + mContext.getResources().getDimensionPixelSize(R.dimen.title_bar_txt_size));
        mPaint.setTextSize(mCommonTextSize);
        mRedBitmap = SkinManager.getResourceBitmap(R.drawable.category_redpoint);
        mRedBitmap = Bitmap.createScaledBitmap(mRedBitmap, SkyConstants.Div(27), SkyConstants.Div(27), true);
    }

    public void addRedPoint(int index, int number) {
        mRedPoint.put(index, number);
        invalidate();
    }

    public void clearRedPoint() {
        mRedPoint.clear();
        invalidate();
    }

    public List<Category> getData() {
        return mCategories;
    }

    public void setData(List<SkyCategoryData> categorys) {
        this.mCategories = new ArrayList<Category>();
        for (int i = 0; i < categorys.size(); i++) {
            Category category = new Category();
            category.mName = categorys.get(i).name;
            category.mWidth = (int) mPaint.measureText(categorys.get(i).name);
            if (i == 0) {
                category.mX = mOffsetX;
            } else {
                category.mX = mCategories.get(i - 1).mX + mCategories.get(i - 1).mWidth
                        + mHSpace * 2;
            }
            LogUtils.d("name width is " + category.mWidth + " and name x is " + category.mX);
            category.mY = mOffsetY;
            category.mTextSize = mCommonTextSize;
            mCategories.add(category);
        }
    }

    private int measureText(String txt) {
        return (int) mPaint.measureText(txt);
    }

    public void setFocusPos(int pos) {
        this.mCurrentPos = pos;
        //invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int i = 0;
        for (Category category : mCategories) {
            mPaint.setTextSize(category.mTextSize);
            LogUtils.d("text x is " + category.mX);
            canvas.drawText(category.mName, category.mX, category.mY, mPaint);
            if (mRedPoint.get(i) != null && mRedPoint.get(i) != 0) {
                mPaint.setTextSize(mRedTextSize);
                String txt = String.valueOf(mRedPoint.get(i));
                int txtWidth = measureText(txt);
//                Rect rect = new Rect(category.mX + category.mWidth, SkyConstants.Div(14), 
//                        category.mX + category.mWidth + txtWidth + SkyConstants.Div(30),SkyConstants.Div(60));
                canvas.drawBitmap(mRedBitmap, category.mX + category.mWidth, SkyConstants.Div(14), null);
                //canvas.drawText(txt, category.mX + category.mWidth + SkyConstants.Div(30) / 2 - 2, SkyConstants.Div(45), mPaint);
            }
            i++;
        }
    }

    public void destory() {
        if (mRedBitmap != null && !mRedBitmap.isRecycled()) {
            mRedBitmap.recycle();
            mRedBitmap = null;
        }
    }

    public class Category {
        public int mWidth;
        public int mHight;
        public String mName;
        public int mTextSize;
        public int mX;
        public int mY;
    }
}
